package com.jwb.util;


import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

import com.alibaba.fastjson.JSONObject;

public class AES {
	// 加密
    public static String Encrypt(String sSrc, String sKey) throws Exception {
        if (sKey == null) {
            System.out.print("Key为空null");
            return null;
        }
        // 判断Key是否为16位
        if (sKey.length() != 16) {
            System.out.print("Key长度不是16位");
            return null;
        }
//        byte[] key = new byte[32];
        
        byte[] raw = sKey.getBytes("utf-8");

        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//"算法/模式/补码方式"
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));

        return new Base64().encodeToString(encrypted);//此处使用BASE64做转码功能，同时能起到2次加密的作用。
    }
 
    // 解密
    public static String Decrypt(String sSrc, String sKey) throws Exception {
        try {
            // 判断Key是否正确
            if (sKey == null) {
                System.out.print("Key为空null");
                return null;
            }
            // 判断Key是否为16位
            if (sKey.length() != 16) {
                System.out.print("Key长度不是16位");
                return null;
            }
            byte[] raw = sKey.getBytes("utf-8");

            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            byte[] encrypted1 = new Base64().decode(sSrc);//先用base64解密
            try {
                byte[] original = cipher.doFinal(encrypted1);
                String originalString = new String(original,"utf-8");
                return originalString;
            } catch (Exception e) {
                System.out.println(e.toString());
                return null;
            }
        } catch (Exception ex) {
            System.out.println(ex.toString());
            return null;
        }
    }
    
    /**
     * 解密方法
     *
     * @param encryptedData
     *            要解密的字符串
     * @param keyBytes
     *            解密密钥
     * @param ivStr
     *            向量
     * @return
     */
    public static String decrypt(String encryptedDataStr, String keyBytesStr, String ivStr) {
        byte[] encryptedText = null;
        byte[] encryptedData = null;
        byte[] sessionkey = null;
        byte[] iv = null;
        String originalString="";

        try {
            sessionkey = Base64.decodeBase64(keyBytesStr);
            encryptedData = Base64.decodeBase64(encryptedDataStr);
            iv = Base64.decodeBase64(ivStr);
            SecretKeySpec skeySpec = new SecretKeySpec(sessionkey, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(iv));
            encryptedText = cipher.doFinal(encryptedData);
            originalString = new String(encryptedText,"utf-8");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return originalString;
    }
 
    public static void main(String[] args) throws Exception {
        /*
         * 此处使用AES-128-ECB加密模式，key需要为16位。
         */
//        String cKey = "123$567*1@34%678";
//        JSONObject jsobject = new JSONObject();
//        jsobject.put("e_id", "16");
//        jsobject.put("code", "123$567*1@34%678");
//        // 需要加密的字串
//        String cSrc = "{\"e_id\":\"16\",\"code\":\"123$567*1@34%678\"}";
//        System.out.println(cSrc);
//        System.out.println(jsobject.toString());
//        // 加密
//        String enString = AES.Encrypt(cSrc, cKey);
//        System.out.println("加密后的字串是：" + enString);
//        String enString1 = AES.Encrypt(jsobject.toString(), cKey);
//        System.out.println("加密后的字串是：" + enString1);
//        String sss="zuD90KA5t9jYbJbOQPcMozipf4BVefRXZgEq5gEeI7kyojxU7jynCWZHO3Ut7eCi8k+EaHCLND/OOEC4/8RTk9FXzNUiGhAr91F9/WNMhIxh6267edmdz58wrof894hDWndmslecJ+hd8YYm10z81xGBYf3VeweMzeGMdlT+imaMNoiWXdaso7l0tlF3EnCdw+FCELWmjsmTyqHLd8TgJI0GtSSzhNvlihVUT6irc0bdUNo5XhFWNBUlMxI5am0iB1NcRIs3ZrWNuZi5nTMthWa0PZbE9QWu6+uuj6tUhgtNQX7/ygVL1tCUHTRfnIOF1s/FFyKk5cuklWmhNaU4pFQxpvlmKPfCO2LxivRVPnSWwW5q8vyF2Z4VggtxzkpiedvLFIyks5sXFRo0yel6APPcSVHbW8ZGedTnqTYVWuY=";
//        // 解密
//        String DeString = AES.Decrypt(enString, cKey);
//        System.out.println("解密后的字串是：" + DeString);
//        String DeString1 = AES.Decrypt(sss, cKey);
//        System.out.println("解密后的字串是：" + DeString1);
    	String sss="I3OX42DDAXdc5n4dxKGPd1uC+ipLMEFuCa0/eUkVTtNte/LP0ukK0B2VmTlatHohD8yh5NOsydO0uTaWzGsvNmfT4Uh14PjP2xb6eNMxODI5z5ipbLcSSvv5XMfyxnj2DY82MTByxjpP/94cipEE3t2xnLr0Ozz0F6tOnPspssuf+A+pkt/SIs9wH4k8wto6zcDjRwhe7pZYkl9aMmdgax7Z95ff8/1+m2e8lyl8J3FVMVy0HQLFE2mwfE2bViPqVtmZ2XzGhRTBxCE9rY141JVzodTxp2mlIvLoxryDQBKB5PT+7ZOav2Sc7GPGvj7iNPLkE4acVnoRuHqz9sPuqRSlW8fhUEwDZN6U8xsjyxUJJDDMn7Vs9mH6dnkOJ6HvCCw1GhHGIKSiwuJor0j/g/CDyBmPE3QB0Q81zw1zFM0=";
    	String DeString1 = AES.decrypt(sss, "/gTtX7erLMExKeG0cx0I7w==","KEurvvKR6QDB7mwhBUwSNg==");
        System.out.println("解密后的字串是：" + DeString1);
        JSONObject jsobject =JSONObject.parseObject(DeString1);
        System.out.println("当天步数：" + jsobject.getJSONArray("stepInfoList").getJSONObject(jsobject.getJSONArray("stepInfoList").size()-1).getString("step"));
    }

}
